package hsmoto.supportClass
{
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	import hsmoto.core.Tool;
	import hsmoto.core.UIComponent;
	/**
	 * 垂直布局 
	 * @author Zhang
	 * 
	 */	
	public class VerticalLayout extends LayoutBase
	{
		private static const CHANGER:Object = {left:"left", right:"right", center:"horizontalCenter", normal:"normal"};
		
		public static const LEFT:String = "left";
		public static const CENTER:String = "center";
		public static const RIGHT:String = "right";
		public static const NORMAL:String = "normal";
		/**
		 *布局元素之间的垂直间隔 
		 */		
		private var _gap:int;
		/**
		 *水平方向的对齐方式 
		 */		
		private var _horizontalAlign:String;
		
		public function VerticalLayout(gap:int = 10)
		{
			_gap = gap;
			_horizontalAlign = LEFT;
		}
		
		override public function needUpdate():Boolean
		{
			var rect1:Rectangle = measure();
			var rect2:Rectangle = targetRect;
			var changed:Boolean;
			if(_horizontalAlign == "left" || _horizontalAlign == "normal")
			{
				changed = rect1.height != rect2.height || rect1.y != rect2.y;
			}
			else
			{
				changed = rect1.width != rect2.width || rect1.height != rect2.height || rect1.x != rect2.x || rect1.y != rect2.y;
			}
			targetRect = rect1;
			return changed;
		}
		/**
		 * 让container的子元件按垂直方式排序
		 */		
		override public function updateDisplayList():void
		{
			if(!this.target)
				return;
			var child:DisplayObject, height:int, rect:Rectangle, num:int = this.target.numChildren;
			this.targetRect = measure();
			for (var i:int = 0; i < num; i++)
			{
				child = this.target.getChildAt( i );
				rect = Tool.getScaleBounds(child);
				if(child is UIComponent)
				{
					Tool[_horizontalAlign](child as UIComponent, 0, rect, this.targetRect);
					(child as UIComponent).currentY = i * _gap + height - rect.y;
				}
				else
				{
					Tool[_horizontalAlign](child, 0, rect, this.targetRect);
					child.y = i * _gap + height - rect.y;
				}
				height += rect.height;
			}
		}
		
		override protected function measure():Rectangle
		{
			if(this.target.scrollRect)
				return Tool.getScaleBounds(this.target);
			var child:DisplayObject, rect:Rectangle, num:int = this.target.numChildren, height:int;
			for (var i:int = 0; i < num; i++)
			{
				child = this.target.getChildAt( i );
				rect = Tool.getScaleBounds(child);
				Tool.addToRect(0 - rect.x, i * _gap + height - rect.y, rect.width, rect.height);
				height += rect.height;
			}
			return Tool.getRect(this.target);
		}
		/**
		 * 布局元素之间的垂直间隔
		 */		
		public function get gap():int
		{
			return _gap;
		}
		
		public function set gap(value:int):void
		{
			_gap = value;
			if(this.target)
				this.target.addToUpdateDisplayList();
		}
		/**
		 *水平方向的对齐方式 
		 */		
		public function set horizontalAlign(value:String):void
		{
			_horizontalAlign = CHANGER[value];
			if(this.target)
				this.target.addToUpdateDisplayList();
		}

		public function get horizontalAlign():String
		{
			return _horizontalAlign;
		}
	}
}